#include "employclient.h"
#include "ui_employclient.h"

// tcpSocket new一个堆区的对象
QTcpSocket *tcpSocket = new QTcpSocket(NULL);

employclient::employclient(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::employclient)
{
    ui->setupUi(this);
    login = new loginDialog;


    //初始化连接服务器按钮的状态
    connectServerBtn_statues = false;
    this->setWindowTitle("基于tcp的员工管理系统客户端");

    //禁止sendMsgBtn、updateBtn、workNumEdit等
    ui->sendSaveMsgBtn->setEnabled(false);
    ui->updateMsgBtn->setEnabled(false);
    ui->readMsgBtn->setEnabled(false);
    ui->workNumEdit->setEnabled(false);
    ui->nameEdit->setEnabled(false);
    ui->sexEdit->setEnabled(false);
    ui->birthEdit->setEnabled(false);
    ui->postEdit->setEnabled(false);
    ui->salaryEdit->setEnabled(false);

    //连接服务器成功，触发connected的信号函数。
    connect(tcpSocket,&QTcpSocket::connected,
            this,&employclient::on_connected_slots);
    //与服务器断开成功，触发一个disconnected()de 信号函数
    connect(tcpSocket,&QTcpSocket::disconnected,
            this,&employclient::on_disconnected_slots);
    //客户端接收到服务器消息，会触发一个readyRead()的信号函数
    connect(tcpSocket,&QTcpSocket::readyRead,
            this,&employclient::on_readyRead_slots);
}

employclient::~employclient()
{
    delete ui;
}


void employclient::on_connectServerBtn_clicked()
{
    //如果是没有连接服务器的状态，可以连接服务器
    if(connectServerBtn_statues == false) {
        //连接到服务器
        port = ui->portEdit->text().toUShort();
        QString ip_str = ui->ipEdit->text();
        if (hostAddress.setAddress(ip_str)) {
            qDebug() << "ip地址有效";
        }else {
            qDebug() << "ip地址无效";
            return;
        }
        tcpSocket->connectToHost(hostAddress,port);
    }
    //连接服务器的状态，可以断开连接服务器
    else{
        connectServerBtn_statues = false;
        //修改断开服务器按钮中的文本，修改为“连接服务器”
        ui->connectServerBtn->setText(tr("连接服务器"));
        tcpSocket->disconnectFromHost();
        // 如果和服务器断开连接成功会触发一个disconnected()的信号函数
    }
}

void employclient::on_connected_slots()
{
    ui->ipEdit->setEnabled(false);
    ui->portEdit->setEnabled(false);
    connectServerBtn_statues = true;
    //修改连接服务器按钮中的文本，修改为“断开服务器”
    ui->connectServerBtn->setText(tr("断开服务器"));
    login->show();
}

void employclient::on_disconnected_slots()
{
    //使能userNameEdit, portEdit, ipEdit
    ui->portEdit->setEnabled(true);
    ui->ipEdit->setEnabled(true);

    //禁止sendMsgBtn、UpdateBtn、WorkNumEdit等
    ui->sendSaveMsgBtn->setEnabled(false);
    ui->updateMsgBtn->setEnabled(false);
    ui->readMsgBtn->setEnabled(false);
    ui->birthEdit->setEnabled(false);
    ui->nameEdit->setEnabled(false);
    ui->sexEdit->setEnabled(false);

}

void employclient::on_readyRead_slots()
{
    //接收服务器发送给客户端的消息
    QByteArray msg = tcpSocket->readAll();
    //将消息显示到listWidget窗口
    qDebug("第一个字符为：%c",(char)msg[0]);
    qDebug() << "msg = " << QString::fromLocal8Bit(msg);
    char ss = '^';
    QList<QByteArray> strlist = msg.split(ss);

    if((QString)strlist[0] == '1')
    {
        qDebug("登录成功");
        login->close();

        ui->workNumEdit->setText(strlist[1]);
        ui->nameEdit->setText(strlist[2]);
        ui->sexEdit->setText(strlist[3]);
        ui->birthEdit->setText(strlist[4]);
        ui->postEdit->setText(strlist[5]);
        ui->salaryEdit->setText(strlist[6]);
        ui->updateMsgBtn->setEnabled(true);
        ui->readMsgBtn->setEnabled(true);
    }
    if((QString)strlist[0]=='2')
    {
        QMessageBox::critical(
                    nullptr,
                    "登录失败",
                    "账号或者密码错误！",
                    QMessageBox::Yes);
        login->show();
    }
    if((QString)strlist[0] == '3')
    {
        ui->workNumEdit->setText(strlist[1]);
        ui->nameEdit->setText(strlist[2]);
        ui->sexEdit->setText(strlist[3]);
        ui->birthEdit->setText(strlist[4]);
        ui->postEdit->setText(strlist[5]);
        ui->salaryEdit->setText(strlist[6]);
    }

}

void sendMessage(const QByteArray &msg)
{
    tcpSocket->write(msg);
}

void employclient::on_updateMsgBtn_clicked()
{
    //使能sendMsgBtn、UpdateBtn、NameEdit等
    ui->sendSaveMsgBtn->setEnabled(true);
    ui->birthEdit->setEnabled(true);
    ui->nameEdit->setEnabled(true);
    ui->sexEdit->setEnabled(true);

}


void employclient::on_sendSaveMsgBtn_clicked()
{
    // 获取员工信息
    workNum = ui->workNumEdit->text();
    name = ui->nameEdit->text();
    sex = ui->sexEdit->text();
    birth = ui->birthEdit->text();
    post = ui->postEdit->text();
    salary = ui->salaryEdit->text();
    // 整合员工信息并发送
    QString msg = "4^@^" + workNum + "^@^" + name + "^@^" + sex + "^@^";
    msg = msg + birth + "^@^" + post + "^@^" + salary + "^@^";
    sendMessage(msg.toLocal8Bit());

}


void employclient::on_readMsgBtn_clicked()
{
    // 整合读取员工信息指令并发送
    QString msg = "3^@^" + workNum + "^@^";
    sendMessage(msg.toLocal8Bit());
}

